<template>
	<view class="seed-goods">
		<!-- 收货地址 -->
		<view class="top baseW">
			<view class="top-left">
				<view class="left-icon">收</view>
			</view>
			<view class="top-info">
				<view style="margin-bottom: 10rpx;">
					<text style="font-size: 15px; font-weight: 700;">{{orderInfo.contacts}}</text>
					<text style="margin-left: 20rpx;">{{orderInfo.phone}}</text>
				</view>
				<view class="address">
					<text style="color: #9e9e9e;">{{orderInfo.addressName + orderInfo.houseNum}}</text>
				</view>
			</view>
			<view class="top-right" @click="copy()">
				<view class="iconfont icon-copy"></view>
			</view>
		</view>

		<!-- 物流信息 -->
		<view class="logistice baseW">
			<!-- 快递单号 -->
			<view class="item">
				<view class="item-left">
					<text>快递单号</text>
				</view>
				<view class="item-right">
					<u-input type="text" v-model="num" placeholder="输入快递单号" input-align="right"/>
					<u-icon name="scan" color="#2ba4f7" size="36" class="right-right" style="margin-top: -5rpx;"  @click="scan"></u-icon>
				</view>
			</view>
			<!-- 物流公司 -->
			<view class="item">
				<view class="item-left">
					<text>物流公司</text>
				</view>
				<view class="item-right">
					<u-input type="select" v-model="companyName" placeholder="请选择物流公司" :select-open="show"  @click="show = true" input-align="right"/>
				</view>
			</view>
		</view>
		
		<view class="submit-btn" @click="submit" v-if="this.id == 0">立即发货</view>
		<view class="submit-btn" @click="submit" v-else>确认修改</view>
		<!-- 物流选择器 -->
		<u-select title="请选择物流" mode="single-column" v-model="show" :list="list" @confirm="selectCom"></u-select>
	</view>
</template>

<script>
	import com from '@/common/js/database/order.js'
	export default {
		data() {
			return {
				num:'',
				company:'',
				companyName:'',
				id:0,                   //物流信息中含有该id
				orderId:'',
				businId:'',
				orderInfo:{},           //订单买家信息
				flag:true               ,//防止重复提交
				show: false,
				list: com.logisticsCom
			};
		},
		onLoad() {
			//嵌套$emit传数据对象
			uni.$once('order',(data) => {
				this.orderId = data.orderId;
				this.businId = data.businId;
				this.orderInfo = data.orderInfo;
				if(data.logistics){          //已发货，有物流信息
					this.id = data.logistics.id;
					this.num = data.logistics.logisticsNum;
					this.company = data.logistics.logisticsCom.value
				}
			});
			uni.$emit('need2');
			//保证orderId和businId不能为空
			if(!this.orderId || !this.businId){
				this.$tip.toast('数据获取失败，请重试');
				setTimeout(function() {
					uni.navigateBack()
				},1000)
			}
			this.list.forEach(item =>{
				if(item.value == this.company){
					return this.companyName = item.label
				}
			})
		},
		methods: {
			//扫一扫识别单号
			scan() {
				let that = this;
				uni.scanCode({
					success(res) {
						that.num = res.result
					}
				});
			},
			selectCom(e){
				this.company = e[0].value;
				this.companyName = e[0].label
			},
			//复制内容
			copy() {
				let value = this.orderInfo.contacts + this.orderInfo.phone + this.orderInfo.addressName + this.orderInfo.houseNum;
				uni.setClipboardData({data: value});
			},
			//提交
			submit(){
				if(!this.flag) return this.$tip.toast('手速太快了,悠着点~');
				this.flag = false;
				
				if(!this.num) return this.$tip.toast('请输入快递单号！');
				if(!this.company) return this.$tip.toast('请选择物流公司！');
				//快递单号只能包含英文和数字
				if(!this.$u.test.enOrNum(this.num)) return this.$tip.toast('请输入正确快递单号！');
				//物流公司简称不能含有中文
				if(escape(this.company).indexOf("%u")>=0) return this.$tip.toast("请输入正确的物流公司英文简称！");
				
				//通过id是否为零判断时立即发货还是修改物流信息
				this.id == 0 ? this.seedGoods() : this.editSeedGoods()
			},
			//立即发货
			seedGoods(){
				this.$http.post('/api/auth/order/sendGoods',{businId: this.businId,orderId: this.orderId,logisticsCom: this.company,logisticsNum: this.num}).then(res => {
					setTimeout(function() {
						this.flag = true
					},1000);
					this.$tip.successRedirect(res,1000,'../order-manage?tab=1')
				})
			},
			//编辑物流
			editSeedGoods(){
				this.$http.post('/api/auth/order/editSendGoods',{id: this.id,logisticsCom: this.company,logisticsNum: this.num}).then(res => {
					setTimeout(function() {
						this.flag = true
					},1000);
					this.$tip.successRedirect(res,1000,'../order-manage?tab=1','修改成功')
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.top {
		display: flex;
		align-items: center;
		justify-content: space-around;
		margin: 20rpx;
		padding: 30rpx 0;

		.top-left {
			padding-left: 10rpx;

			.left-icon {
				height: 70rpx;
				width: 70rpx;
				line-height: 70rpx;
				text-align: center;
				color: #fff;
				border-radius: 35rpx;
				background-color: #2ba4f7;
			}
		}

		.top-info {
			width: 70%;
		}

		.top-right {
			padding-right: 20rpx;
		}
	}
	
	.logistice{
		margin: 20rpx;
		padding: 0;
	}

	.item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 40rpx;
		height:120rpx;
		font-size: 15px;
		border-bottom: 1px solid #f6f7f9;
		.item-right{
			display: flex;
			align-items: center;
			font-size: 14px;
			.right-right{
				margin-left: 20rpx;
			}
		}
	}
	
	.submit-btn{
		position: fixed;
		bottom: 20rpx;
		margin: 0 5%;
		width: 90%;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		color: #fff;
		font-size: 15px;
		border-radius: 40rpx;
		background-color: #2ba4f7;
	}
</style>
